﻿using AutoMapper;
using MediatR;
using SlaughterItem.API.Application.Commands.Week03.leo;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week03.wenk
{
    public class OilyfatCreateHandler : IRequestHandler<OilyfatCreateCommend, Result>
    {
        private readonly IBaseRepository<Oilyfat> Oilyfat;
        private readonly IMapper mapper;

        public OilyfatCreateHandler(IMapper mapper, IBaseRepository<Oilyfat> Oilyfat)
        {
            this.mapper = mapper;
            this.Oilyfat = Oilyfat;
        }

        public async Task<Result> Handle(OilyfatCreateCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Oilyfat oilyfat = mapper.Map<Oilyfat>(request);
                var res = Oilyfat.Add(oilyfat);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
